package xyz.xmh.pattern.j2ee.interceptingfilterpattern;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器链
 *
 * @author admin
 */
public class FilterChain {

    /**
     * 过滤器集合
     */
    private final List<Filter> filterList = new ArrayList<>();

    /**
     * 目标对象
     */
    private final Target target;

    public FilterChain(Target target) {
        this.target = target;
    }


    /**
     * 添加过滤器
     *
     * @param filter 过滤器
     * @return true添加成功，反之失败
     */
    public boolean addFilter(Filter filter) {
        return filterList.add(filter);
    }

    /**
     * 过滤器处理请求
     *
     * @param request 请求
     */
    public void execute(String request) {
        for (Filter filter : filterList) {
            filter.execute(request);
        }
        target.execute(request);
    }

}
